package com.macro.mall.controller;

import com.github.pagehelper.PageInfo;
import com.macro.mall.common.vo.Result;
import com.macro.mall.entity.Admin;
import com.macro.mall.entity.AdminParam;
import com.macro.mall.entity.AdminQuery;
import com.macro.mall.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Xiao Hai
 * @since 2021-04-03
 */
@Api(tags = "获取管理员信息")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation(value = "分页查询管理员信息")
    @GetMapping("/getAdmin")
    public Result getAdmin(@ApiParam(name = "adminQuery", value = "管理员查询对象", required = false) AdminQuery adminQuery){

        if (adminQuery.getPageNum() == 0 && adminQuery.getPageSize() == 0){
            return Result.error().message("分页对象不能为空");
        }
        PageInfo<Admin> adminPage = adminService.getAdmin(adminQuery);

        return Result.ok().data("adminPage",adminPage);
    }




    @ApiOperation(value = "永久禁言管理员")
    @PostMapping("/updateAdminStatus/{id}")
    public Result updateAdminStatus(@ApiParam(name="id",value = "管理员id",required = true)@PathVariable int id){

        int i = adminService.updateAdminStatus(id);

        if (i > 0){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败");
    }


    @ApiOperation(value = "永久删除管理员")
    @PostMapping("/updateAdminDeleted/{id}")
    public Result updateAdminDeleted(@ApiParam(name="id",value = "管理员id",required = true)@PathVariable int id){

        int i = adminService.updateAdminDeleted(id);

        if (i > 0){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败");

    }




    @ApiOperation(value = "通过id修改管理员信息")
    @PostMapping("/updateAdmin/{id}")
    public Result updateAdmin(@ApiParam(name = "id", value = "管理员id", required = true)@PathVariable int id,@ApiParam(name = "adminParam", value = "管理员", required = false)@RequestBody AdminParam adminParam){

        int i = adminService.updateAdmin(id,adminParam);

        if (i > 0){

            return Result.ok().message("修改成功");
        }
        return Result.error().message("修改失败");
    }



    @ApiOperation(value = "新增管理员")
    @PostMapping("/saveAdmin")
    public Result saveAdmin(@ApiParam(name = "adminParam", value = "管理员", required = true)@RequestBody AdminParam adminParam){

        int i= adminService.saveAdmin(adminParam);

        if (i > 0 ){
            return Result.ok().message("新增成功");
        }

        return Result.error().message("新增失败");
    }

}

